*! version  1.1  16Nov2023
*! Version: 1.0  2jan2008
*! Author: Yujun Lian, arlionn@163.com


/* cap program drop peermean */
program define peermean
version 13

  syntax varlist(min=1 numeric ) [if] [in], by(varlist) [Suffix(string)] 
	
	*local suffix "`gen'"
	if `"`suffix'"' == ""{
	   local suffix="_peer"   
	}
  
* Validate suffix
   foreach k of varlist `varlist' {
	 capture confirm variable `k'`suffix', exact
	 if _rc == 0 {
		di as error "Variable `k'`suffix' already existed, Change another Suffix instead of `suffix'."
		exit 111
	 }
   }	

   	marksample touse
	qui count if `touse'
	if r(N)==0{ 
	   error 2000 
	} 
* 
qui{
  foreach y of varlist `varlist'{
    tempvar ymean Ng w1 w2
	bysort `by':  gen `Ng' = _N if `touse'
	bysort `by': egen `ymean' = mean(`y') if `touse'
	gen double `w1' = . 
	gen double `w2' = .
	replace `w1' = `Ng'/(`Ng'-1) if `Ng'>1&`touse'
	replace `w2' =    1/(`Ng'-1) if `Ng'>1&`touse'
	replace `w1' = 0 if `Ng'==1&`touse'
    replace `w2' = 0 if `Ng'==1&`touse'
	
	gen double `y'`suffix' = `w1'*`ymean' - `w2'*`y' if `touse'
	
	label var `y'`suffix' "Peer mean of `y'"
  }  
}
  
end 


*-formula
/*
               1
  y[peerx] = -----(y1+y2+...+y[i-1]+y[i+1]+...+y[N])
              N-1
			  
               1
           = -----(y1+y2+...+y[i-1]+y[i+1]+...+y[N] + y[i] - y[i])
              N-1	
			  
               N               1  
           = -----(y_mean) - ----- y[i]
              N-1			  N-1 
			  
		   = w1*y_mean - w2*y_i	  
*/
